package com.chatroom.utils;

import com.chatroom.enity.ResultMessage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author 计算机系 ITAEM 纱雾休比我老婆
 * @Description 用于封装消息的工具类
 * @date 2022/8/9 1:20
 */
public class MessageUtil {

    public static String getMessage(boolean isSystemMessage,String fromName,Object message){
        try {
            ResultMessage resultMessage = new ResultMessage();
            resultMessage.setIsSystem(isSystemMessage);
            resultMessage.setMessage(message);
            if(fromName != null){
                resultMessage.setFromName(fromName);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.writeValueAsString(resultMessage);
        } catch (JsonProcessingException e){
            e.printStackTrace();
        }
        return null;
    }
}
